#pragma once
#include <QByteArray>
#include <QQueue>
#include <QMap>
#include <QDebug>

struct MSG {
    enum Type {
        // 服务端接收
        CREATE,
        DELETE,
        JOIN,
        LEAVE,
        INVITE,
        QUERY,
        // 服务端转发
        VIDEO,
        AUDIO,
        TEXT,
        PING,
        // 客户端接收
        CREATED,
        JOINED,
        OTHER_JOINED,
        OTHER_LEAVED,
        HISTORY,
        INVITATION,
        EXPERTS,
        //数据库
        LOGIN,
        REGIETER,
        DEL_INV,
        DB_STATUS,
        // 空
        UNKNOWN
    };

    Type type;
    quint16 userId;
    QByteArray data;

    MSG() : type(UNKNOWN), userId(0) {}

    inline void setType(const char* data) {
        type = type_map.key(data, UNKNOWN);
    }

    inline const char* getType() const {
        return type_map.value(type, "UNKNOWN");
    }

private:
    inline static QMap<Type, QByteArray> type_map = {
        {CREATED, "CREATED"},
        {CREATE, "CREATE"},
        {DELETE, "DELETE"},
        {JOINED, "JOINED"},
        {JOIN, "JOIN"},
        {LEAVE, "LEAVE"},
        {INVITE, "INVITE"},
        {QUERY, "QUERY"},
        {VIDEO, "VIDEO"},
        {AUDIO, "AUDIO"},
        {TEXT, "TEXT"},
        {OTHER_JOINED, "OTHER_JOINED"},
        {OTHER_LEAVED, "OTHER_LEAVED"},
        {HISTORY, "HISTORY"},
        {INVITATION, "INVITATION"},
        {EXPERTS, "EXPERTS"},
        {LOGIN, "LOGIN"},
        {REGIETER, "REGIETER"},
        {DEL_INV, "DEL_INV"},
        {DB_STATUS, "DB_STATUS"},
        {PING, "0"}
    };
};

using MsgQueue = QQueue<MSG>;

namespace MessageHelper {
    MSG makeMessage(const QByteArray& data);
    QByteArray readMessage(const MSG& msg);

    MSG makeTextMessage(const QString& text);
    MSG makeMediaMessage(const QByteArray& data, MSG::Type type, quint16 userId = 0);
    QByteArray makePingMessage();
    QByteArray getData(const QByteArray& rawData);
}
